package com.cherry.mvc.util;


import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 默认的日期格式是yyyy/MM/dd
 * 这个是自定义日期格式
 */
public class StringToDateConverter implements Converter<String, Date> {
    @Override
    public Date convert(String s) {
        if (s == null || s.length() == 0) {
            throw new RuntimeException("请您传入数据");
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return format.parse(s);
        } catch (ParseException e) {
            throw new RuntimeException("日期格式错误");
        }
    }
}
